import Vue from 'vue'
import VueRouter from 'vue-router'
import stroe from '../store/index'
import { Message } from "element-ui";
import Home from '../views/admin/Home.vue'
import Yuangongguanli from '../views/admin/yuangongguanli/Yuangongguanli.vue'
import Shangpingunali from '../views/admin/Shangpingunali.vue'
import Xiaoshouguanli from '../views/admin/Xiaoshouguanli.vue'
import Cuxiaohuodong from '../views/admin/Cuxiaohuodong.vue'
import Shanghuo from '../views/cashier/Shanghuo.vue'
import Shouyin from '../views/cashier/Shouyin.vue'
import Login from '../views/Login.vue'
import Home2 from '../views/cashier/Home.vue'

Vue.use(VueRouter)

const routes = [{
        path: '/home1',
        name: 'Home',
        component: Home,
        meta: {
            isLogin: true,
            role: ['99']
        },
        children: [{
                path: 'yuangongguanli',
                name: 'Yuangongguanli',
                component: Yuangongguanli,
                meta: {
                    isLogin: true,
                    role: ['99']
                },
            },
            {
                path: 'shangpingunali',
                name: 'Shangpingunali',
                component: Shangpingunali,
                meta: {
                    isLogin: true,
                    role: ['99']
                },
            },
            {
                path: 'xiaoshouguanli',
                name: 'Xiaoshouguanli',
                component: Xiaoshouguanli,
                meta: {
                    isLogin: true,
                    role: ['99']
                },
            },
            {
                path: 'cuxiaohuodong',
                name: 'Cuxiaohuodong',
                component: Cuxiaohuodong,
                meta: {
                    isLogin: true,
                    role: ['99']
                },
            },

        ]
    },
    {
        path: '/home2',
        name: 'Home2',
        component: Home2,
        meta: {
            isLogin: true,
            role: ['99', '1']
        },
        children: [{
                path: 'shanghuo',
                name: 'Shanghuo',
                component: Shanghuo,
                meta: {
                    isLogin: true,
                    role: ['99', '1']
                },
            },
            {
                path: 'shouyin',
                name: 'Shouyin',
                component: Shouyin,
                meta: {
                    isLogin: true,
                    role: ['99', '1']
                },
            },
        ]
    },

    {
        path: '/',
        name: 'Login',
        component: Login,
        meta: {
            isLogin: false,
            role: ['99', '1']
        },
    }

]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

router.beforeEach((to, from, next) => {
    if (!to.meta.isLogin) {
        next();
    } else {
        if (stroe.state.user.username) {
            // 现在我们要处理的问题是  我需要将登录的状态或者是信息保存下来
            // 登录  
            if (to.meta.role.includes(stroe.state.user.operation_Level)) {
                next();
            } else {
                Message({
                    message: '您没有权限进入该页面！',
                    type: 'error'
                });
                router.back(1)
            }
        } else {
            Message({
                message: '登录信息过期，请重新登录！',
                type: 'error'
            });
            router.push('/')
        }
    }
})

export default router